﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace VirtualPress.Tool.Configration
{
    public class SiteResource
    {

        static Resource resource = null;
        static SiteResource()
        {
            XElement xe = XElement.Load(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"/content/config/sitesetting.xml");
            resource = new Resource()
            {
                Host = new Resource_Host()
                {
                    Domain = xe.Element("host").Element("domain").Value,
                    SiteName = xe.Element("host").Element("name").Value
                },
                Setting = xe.Element("host").Element("setting").Elements()
                        .ToDictionary(
                            xn => { return xn.Name.ToString(); },
                            xn => { return xn.Value; }),
                Company = xe.Element("host").Element("company").Elements()
                    .ToDictionary(
                        xn => { return xn.Name.ToString(); },
                        xn => { return xn.Value; }),
                seo = new SEOSettings()
                {
                    Default = new SEOSettingItem() { 
                       title =  xe.Element("seo").Element("default").Attribute("title").Value,
                       keywords = xe.Element("seo").Element("default").Attribute("keyword").Value,
                       description = xe.Element("seo").Element("default").Attribute("description").Value
                    },
                    PageSet=xe.Element("seo").Elements("item")
                    .ToDictionary(
                        xn=>{ 
                            return xn.Attribute("name").Value; 
                        },
                        xn=>{
                            SEOSettingItem item = new SEOSettingItem();
                            item.title = xn.Attribute("title").Value;
                            item.keywords = xn.Attribute("keyword").Value;
                            item.description = xn.Attribute("description").Value;
                            return item;
                        })
                },
                Press = new PressSetting()
                {
                    defaultExamineSheet = xe.Element("press").Element("defaultExamineSheet").Value,
                    defaultExaminePrice = Int32.Parse(xe.Element("press").Element("defaultExaminePrice").Value),
                    defaultManuscritpsTemplete = xe.Element("press").Element("defaultManuscritpsTemplete").Value,
                    manuscriptsPrice = new ManuscriptsPrice()
                    {
                        defaultPageSize = Int32.Parse(xe.Element("press").Element("manuscritpsPage").Element("PageSize").Value),
                        defaultPrice = Int32.Parse(xe.Element("press").Element("manuscritpsPage").Element("Price").Value),
                        pricePerPage = Int32.Parse(xe.Element("press").Element("manuscritpsPage").Element("PricePerPage").Value),
                        publicationPrice = Int32.Parse(xe.Element("press").Element("manuscritpsPage").Element("PublicationPrice").Value)
                    }

                },
                display = new Display()
                {
                    homeJournals = xe.Element("display").Element("homejournals").Value
                }
            };
        }

        #region 字段
        public static string Domain
        {
            get
            {
                return resource.Host.Domain;
            }
        }
        public static string SiteName
        {
            get
            {
                return resource.Host.SiteName;
            }
        }
        public static string CompanyAddress
        {
            get
            {
                return resource.Company["address"];
            }
        }
        public static string CompanyPhone
        {
            get
            {
                return resource.Company["phone"];
            }
        }
        public static string Logo
        {
            get
            {
                return resource.Setting["logo"];
            }
        }
        /// <summary>
        /// 默认稿件模板
        /// </summary>
        public static string DefaultManuscritpsTemplete
        {
            get
            {
                return resource.Press.defaultManuscritpsTemplete;
            }
        }
        public static string DefaultExamineSheet
        {
            get {
                return resource.Press.defaultExamineSheet;
            }
        }
        /// <summary>
        /// 默认专审最低费用
        /// </summary>
        public static int DefaultExaminePrice
        {
            get
            {
                return resource.Press.defaultExaminePrice;
            }
        }
        /// <summary>
        /// 默认页面数
        /// </summary>
        public static int DefaultPageSize
        {
            get
            {
                return resource.Press.manuscriptsPrice.defaultPageSize;
            }
        }
        /// <summary>
        /// 不超过默认页面数价格
        /// </summary>
        public static int DefaultPagePrice
        {
            get
            {
                return resource.Press.manuscriptsPrice.defaultPrice;
            }
        }
        /// <summary>
        /// 超过默认页面数，每页价格
        /// </summary>
        public static int DefaultPricePerPage
        {
            get
            {
                return resource.Press.manuscriptsPrice.pricePerPage;
            }
        }
        public static int DefaultPublicationPrice
        {
            get
            {
                return resource.Press.manuscriptsPrice.publicationPrice;
            }
        }
        public static string[] HomeJournals
        {
            get {
                return resource.display.homeJournals.Split(';');
            }
        }
        public static Dictionary<string, Dictionary<string, string>> SeoSettings 
        {
            get {
                Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
                Dictionary<string,string> dict = new Dictionary<string,string>();
                dict.Add("title",resource.seo.Default.title);
                dict.Add("keywords", resource.seo.Default.keywords);
                dict.Add("description", resource.seo.Default.description);
                result.Add("default", dict);
                foreach (var item in resource.seo.PageSet)
                { 
                    dict.Clear();
                    dict.Add("title",item.Value.title);
                    dict.Add("keywords", item.Value.keywords);
                    dict.Add("description", item.Value.description);
                    result.Add(item.Key, dict);
                }
                return result;
            }
        }
        #endregion

        #region
        private class Resource
        {
            public Resource_Host Host { get; set; }
            public Dictionary<string, string> Company { get; set; }
            public Dictionary<string, string> Setting { get; set; }
            public PressSetting Press { get; set; }
            public Display display { get; set; }
            public SEOSettings seo{get;set;}
        }
        private class Resource_Host
        {
            /// <summary>
            /// 站点域名 duo500.com
            /// </summary>
            public string Domain { get; set; }
            /// <summary>
            /// 站点名
            /// </summary>
            public string SiteName { get; set; }
        }
        private class PressSetting
        {
            /// <summary>
            /// 默认稿件模板
            /// </summary>
            public string defaultManuscritpsTemplete { get; set; }
            public string defaultExamineSheet { get; set; }
            /// <summary>
            /// 默认专审最低费用
            /// </summary>
            public int defaultExaminePrice { get; set; }
            /// <summary>
            /// 征稿价格设置
            /// </summary>
            public ManuscriptsPrice manuscriptsPrice { get; set; }
        }
        private class ManuscriptsPrice
        {
            /// <summary>
            /// 默认页面数
            /// </summary>
            public int defaultPageSize { get; set; }
            /// <summary>
            /// 不超过默认页面数价格
            /// </summary>
            public int defaultPrice { get; set; }
            /// <summary>
            /// 超过默认页面数，每页价格
            /// </summary>
            public int pricePerPage { get; set; }
            /// <summary>
            /// 出版费用
            /// </summary>
            public int publicationPrice { get; set; }
        }
        private class Display
        {
            public string homeJournals { get; set; }
        }
        private class SEOSettings
        {
            public SEOSettingItem Default { get; set; }
            public Dictionary<string, SEOSettingItem> PageSet { get; set; }
        }
        private class SEOSettingItem
        {
            public string title { get; set; }
            public string keywords { get; set; }
            public string description { get; set; }
        }
        #endregion
    }
}
